<template>
	<view class="box box-card">
		<uni-forms ref="userForm" :modelValue="userForm" :rules="formRules" errShowType="toast">
			<uni-forms-item name="username">
				<uni-easyinput v-model="userForm.username" placeholder="账号名" maxlength="20"></uni-easyinput>
			</uni-forms-item>
			<uni-forms-item>
				<button class="form-button uni-mt-10" @click="update">保存</button>
			</uni-forms-item>
		</uni-forms>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				userForm: {
					username: ''
				},
				formRules: {
					username: {
						rules: [{
							required: true,
							errorMessage: '请输入账号或手机号'
						}]
					}
				}
			}
		},
		onLoad() {
			this.userForm.username = getApp().globalData.userInfo.username;
		},
		methods: {
			update() {
				this.$refs.userForm.validate().then(() => {
					this.$api.post('/appUser/updateUsername', this.userForm).then(res => {
						uni.reLaunch({
							url: '/pages/my/my',
							success: () => {
								this.$util.messageTip(res.message);
							}
						})
					}).catch(err => {
						this.$util.messageTip(err.message);
					})
				})
			}
		}
	}
</script>
